import { NSteps, type StepsProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type StepsFactoryConstructorType = {
  attrs?: HTMLAttributes;
  props?: StepsProps;
  defaultSlot?: BaseComponentType;
  finishIconSlot?: BaseComponentType;
  errorIconSlot?: BaseComponentType;
};

export class StepsFactory {
  private attrs: HTMLAttributes = {};
  private props: StepsProps = {};
  private defaultSlot: BaseComponentType = null;
  private finishIconSlot: BaseComponentType = null;
  private errorIconSlot: BaseComponentType = null;

  constructor(data?: StepsFactoryConstructorType) {
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.props) this.setProps(data.props);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.finishIconSlot) this.setFinishIcon(data.finishIconSlot);
    if (data?.errorIconSlot) this.setErrorIcon(data.errorIconSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: StepsProps) {
    this.props = props;
    return this;
  }

  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }

  setFinishIcon(finishIconSlot: BaseComponentType) {
    this.finishIconSlot = finishIconSlot;
    return this;
  }

  setErrorIcon(errorIconSlot: BaseComponentType) {
    this.errorIconSlot = errorIconSlot;
    return this;
  }

  create() {
    return (
      <NSteps {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
          "finish-icon":
            this.finishIconSlot != null
              ? () => getSlotsDom(this.finishIconSlot)
              : null,
          "error-icon":
            this.errorIconSlot != null
              ? () => getSlotsDom(this.errorIconSlot)
              : null,
        }}
      </NSteps>
    );
  }
}
